package com.topera.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.topera.commonutils.JwtUtils;
import com.topera.commonutils.R;
import com.topera.commonutils.orderVo.CourseWebVoOrder;
import com.topera.eduservice.client.OrderClient;
import com.topera.eduservice.entity.EduChapter;
import com.topera.eduservice.entity.EduCourse;
import com.topera.eduservice.entity.frontvo.CourseFrontVo;
import com.topera.eduservice.entity.frontvo.CourseWebVo;
import com.topera.eduservice.service.EduChapterService;
import com.topera.eduservice.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/courseFront")
@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private OrderClient orderClient;//订单服务远程调用接口

    /**
     * 获取课程分页信息
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("getCouseFrontInfo/{page}/{limit}")
    public R getCouseFrontInfo(@RequestBody(required = false) CourseFrontVo courseFrontVo,@PathVariable long page, @PathVariable long limit){

        Page<EduCourse> pageCourse = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCouseFrontInfo(pageCourse,courseFrontVo);

        return R.ok().data(map);
    }

    /**
     * 课程详情
     * @param courseId
     * @return
     */
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        List<EduChapter> chapterList = chapterService.getAllChapterBycourseId(courseId);

        //根据课程id 用户id 查看当前课程状态是否已经支付
        boolean isBuy = orderClient.getOrderStatus(courseId, JwtUtils.getMemberIdByJwtToken(request));

        return R.ok().data("chapterList",chapterList).data("courseWebVo",courseWebVo).data("isBuy",isBuy);
    }

    /**
     * 根据课程id获取课程信息
     * @param id
     * @return
     */
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseWebVo,courseOrder);
        return courseOrder;
    }
}
